package com.army.http.slice;

import com.army.http.ResourceTable;
import com.army.http.bean.LaoHuangLi;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel HI_LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x11101, "MainAbilitySlice");
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
        text.setMultipleLine(true);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String currentDate = simpleDateFormat.format(new Date());
                String url = "http://v.juhe.cn/laohuangli/d?date="+currentDate+"&key=7cc85efc122e08f6ad8fa1f26877ddd8";
                text.setText("正在请求网络，请稍后...");

                ZZRHttp.get(url, new ZZRCallBack.CallBackString() {
                    @Override
                    public void onFailure(int i, String s) {
                        text.setText("网络请求失败，请重试...");
                    }

                    @Override
                    public void onResponse(String s) {
                        HiLog.info(HI_LOG_LABEL, s);
                        text.setText("服务器返回数据是：" + s);

                        try {
                            LaoHuangLi laoHuangLi = objectMapper.readValue(s, LaoHuangLi.class);
                            LaoHuangLi.Result result = laoHuangLi.getResult();
                            StringBuffer stringBuffer = new StringBuffer();
                            stringBuffer.append("今日黄历："+"\n");
                            stringBuffer.append("阳历："+result.getYangli()+"\n");
                            stringBuffer.append("阴历："+result.getYinli()+"\n");
                            stringBuffer.append("忌讳："+result.getJi()+"\n");
                            stringBuffer.append("适合："+result.getYi()+"\n");

                            text.setText(stringBuffer.toString());

                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                            text.setText("解析数据失败，请重试...");
                        }

                    }
                });
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
